package com.tzbank.back.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tzbank.back.service.SystemUserService;
import com.tzbank.entity.backstage.SystemUser;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class SystemUserController {

	@Autowired
	private SystemUserService systemUserService;

	/**
	 * 添加用户
	 * @param request
	 * @param systemUser
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@PostMapping(value = "insertSystemUser")
	public Map<String, Object> insertSystemUser(HttpServletRequest request, @RequestBody SystemUser systemUser) {
		try {
			systemUser.setOperator("admin");
			return this.systemUserService.insertSystemUser(systemUser);
		} catch (Exception e) {
			log.error("SystemUserController insertSystemUser error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 删除用户
	 * @param request
	 * @param systemUser
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@PostMapping(value = "deleteSystemUserByAutoId")
	public Map<String, Object> deleteSystemUserByAutoId(HttpServletRequest request,
			@RequestBody SystemUser systemUser) {
		try {
			return this.systemUserService.deleteSystemUserByAutoId(systemUser.getAutoId());
		} catch (Exception e) {
			log.error("SystemUserController deleteSystemUserByAutoId error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 修改用户
	 * @param request
	 * @param systemUser
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@PostMapping(value = "updateSystemUser")
	public Map<String, Object> updateSystemUser(HttpServletRequest request, @RequestBody SystemUser systemUser) {
		try {
			systemUser.setOperator("admin");
			return this.systemUserService.updateSystemUser(systemUser);
		} catch (Exception e) {
			log.error("SystemUserController updateSystemUser error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 查询用户
	 * @param request
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@GetMapping(value = "selectSystemUser")
	public Map<String, Object> selectSystemUser(HttpServletRequest request) {
		try {
			return this.systemUserService.selectSystemUser();
		} catch (Exception e) {
			log.error("SystemUserController selectSystemUser error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 查询用户
	 * @param request
	 * @param autoId
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年5月31日
	 */
	@GetMapping(value = "selectSystemUserByAutoId")
	public Map<String, Object> selectSystemUserByAutoId(HttpServletRequest request,
			@RequestParam(required = true) Integer autoId) {
		try {
			return this.systemUserService.selectSystemUserByAutoId(autoId);
		} catch (Exception e) {
			log.error("SystemUserController selectSystemUserByAutoId error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

}
